package com.res.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.res.share.common.Constants;
import com.res.share.model.ResCollection;
import com.res.share.mapper.ResCollectionMapper;
import com.res.share.model.ResStatistics;
import com.res.share.service.CacheService;
import com.res.share.service.IResCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.res.share.service.IResStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏明细 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class ResCollectionServiceImpl extends ServiceImpl<ResCollectionMapper, ResCollection> implements IResCollectionService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IResStatisticsService resStatisticsService;

    @Override
    public Integer updateArticleCollectionStatus(Integer articleId, Integer userId) {
        Integer result = 0;
        ResCollection resCollection = baseMapper.selectArticleCollection(articleId,userId);
        if (resCollection != null) {
            //取消收藏
            if (resCollection.getIsDel() == 0) {
                int update = baseMapper.updateArticleCollectionStatus(1,userId,resCollection.getId());
                if (update > 0) {
                    //取消收藏
                    result = 1;
                    QueryWrapper queryWrapper=new QueryWrapper();
                    queryWrapper.eq("",userId);
                    ResStatistics resStatistics = resStatisticsService.getOne(queryWrapper);
                    if(resStatistics!=null){
                        if(resStatistics.getCollectionNum()>0){
                            resStatisticsService.updateResCollectionNums(userId,1,1);
                        }
                    }else {
                        resStatistics=new ResStatistics();
                        resStatistics.setUseId(userId);
                        resStatisticsService.save(resStatistics);
                    }
                }
            } else {
                int update = baseMapper.updateArticleCollectionStatus(0,userId,resCollection.getId());
                if (update > 0) {
                    //添加收藏
                    result = 2;
                    resStatisticsService.updateResCollectionNums(userId,2,1);
                }
            }
        } else {
            resCollection = new ResCollection();
            resCollection.setArtId(articleId);
            resCollection.setIsDel(0);
            resCollection.setCreateUserId(userId);
            int insert = baseMapper.insert(resCollection);
            if (insert > 0) {
                //添加收藏
                result = 2;
                resStatisticsService.updateResCollectionNums(userId,2,1);
            }
        }
        //删除缓存
        cacheService.deleteCaches( Constants.RES_NEW_ARTICLE_KEY,Constants.RES_ARTICLE_BASE_KEY,Constants.HOT_ARTICLE_BASE_KEY);
        return result;
    }

    @Override
    public IPage<ResCollection> findCollectionAricle(Page<ResCollection> page, Integer userId) {
        return baseMapper.findCollectionAricle(page,userId);
    }

    @Override
    public void updateBatchDelcollectionArticle(List<String> ids, Integer userId) {
        baseMapper.updateBatchDelcollectionArticle(ids,userId);
        resStatisticsService.updateResCollectionNums(userId,1,ids.size());
        //删除缓存
        cacheService.deleteCaches( Constants.RES_NEW_ARTICLE_KEY,Constants.RES_ARTICLE_BASE_KEY,Constants.HOT_ARTICLE_BASE_KEY);
    }
}
